<template>
  <div class="login-container">
    <!-- 顶部栏 -->
    <van-nav-bar
      class="my-nav-bar"
      title="登录"
      left-text="返回"
      left-arrow
      @click-left="$router.go(-1)"
    />
    <!-- 输入文本 -->
    <van-form @submit="onSubmit">
      <van-field
        v-model="mobile"
        name="mobile"
        label="手机号码"
        placeholder="请输入手机号码"
        :rules="[
          { required: true, message: '请输入手机号码' },
          {
            pattern:
              /^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\d{8}$/,
            message: '手机号格式有误，请重新输入',
          },
        ]"
      />
      <van-field
        v-model="code"
        type="code"
        name="code"
        label="验证码"
        placeholder="请输入验证码"
        :rules="[
          { required: true, message: '请输入验证码' },
          { pattern: /^\d{6}$/, message: '验证码格式不对，请重新输入' },
        ]"
      />
      <div style="margin: 16px">
        <van-button round block type="info" native-type="submit"
      :disabled='isLoading'
      :loading='isLoading'
        loading-text="登陆..."
          >提交</van-button
        >
      </div>
    </van-form>
  </div>
</template>

<script>
//导入接口
import { userLogin } from "@/views/user/user.js";
// 导入token工具
import {saveToken,getToken,removeToken} from '@/utils/token.js'
export default {
  name: "login",
  data() {
    return {
      mobile: "",
      code: "",
      isLoading:false,

    };
  },
  methods: {
    onSubmit(values) {
      this.isLoading=true
      console.log("submit", values);
      userLogin(values)
        .then((res) => {
          console.log(res);
          this.$toast.success("登陆成功");
          saveToken(res.data.data)
          // console.log(getToken());
          // removeToken()
          // 跳转首页
          this.$router.push({path:'/home'})
        })
        .catch((err) => {
           this.$toast.fail("账户或验证码错误");
        })
        .then(()=>{
          this.isLoading=false
        })
    },
  },
};
</script>

<style lang="less">
.login-container {
  .my-nav-bar {
    &.van-nav-bar {
      background-color: #0099fa;
    }
    .van-nav-bar__title {
      color: #fff;
    }
    .van-nav-bar__text {
      color: #fff;
    }
    .van-icon-arrow-left {
      color: #fff;
    }
  }
}
</style>
